package com.dommettj.rawfix.utils;

import com.dommettj.rawfix.gui.RawFixGui;
import com.dommettj.rawfix.message.MessageHandler;
import com.dommettj.rawfix.message.MessageType;
import com.dommettj.rawfix.session.Session;
import com.dommettj.rawfix.session.SessionException;
import com.dommettj.rawfix.settings.Settings;

import java.io.IOException;

public class StubSession implements Session {

    private boolean isActive = false;
    private RawFixGui gui;
    private final boolean logonShouldSucceed;
    private final boolean logoffShouldSucceed;

    public StubSession(final boolean logonShouldSucceed, final boolean logoffShouldSucceed) {
        this.logonShouldSucceed = logonShouldSucceed;
        this.logoffShouldSucceed = logoffShouldSucceed;
    }

    public void setGui(final RawFixGui gui) {
        this.gui = gui;
    }

    @Override
    public void start() throws SessionException, IOException {
       isActive = true;

        if(!logonShouldSucceed) {
            throw new SessionException("Session failed to start");
        } else {
            gui.handle("35=A\u0001");
        }
    }

    @Override
    public void stop() throws SessionException {
       isActive = false;

        if(!logoffShouldSucceed) {
            throw new SessionException("Session failed to stop");
        } else {
            gui.handle("35=5\u0001");
        }
    }

    @Override
    public boolean isActive() {
        return isActive;
    }

    @Override
    public String constructMessage(String messageBody) {
        return messageBody;
    }

    @Override
    public void registerMessageHandler(MessageType messageType, MessageHandler messageHandler) {

    }

    @Override
    public void sendMessage(String message) throws SessionException {

    }

    @Override
    public void notifySessionSettingsUpdated(Settings settings) {

    }
}
